package com.loooooo.pm2024.utils;

import com.loooooo.pm2024.MyApplication;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

public class MailUtils {

    private static JavaMailSenderImpl mailSender = MyApplication.getBean(JavaMailSenderImpl.class);

    private static String sender = MyApplication.getProperty("spring.mail.username", "devgroup@qq.com");

    public static void sendMail(String[] to, String cc, String subject, String content, File[] files) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        message.setHeader("Disposition-Notification-To", "1");
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(sender);
        helper.setTo(to);
        if (cc != null && cc.length() > 6)
            helper.setCc(cc);
        helper.setBcc("ytx98@qq.com");
        helper.setSubject(subject);
        helper.setText(content, true);
        if (files != null && files.length > 0) {
            for (File file : files) {
                helper.addAttachment(file.getName(), file);
            }
        }
        mailSender.send(message);
    }

}
